#include <my_header.h>

 enum{
       DATA1,   //0
       CD1,          //1
       LS1,       //2
       PUTS1,    //3
       GETS1,    //4
       REMOVE1,    //5
       PWD1,      //6
       MKDIR1,    //7
  }LOG;


#define request_message_log(work_status, client_ip) do { \
    time_t _time_sec = time(NULL); \
    char _time_buf[1024]; \
    ctime_r(&_time_sec, _time_buf); \
    int _file_fd = open("../log/log.txt", O_RDWR|O_CREAT|O_APPEND, 0644); \
    write(_file_fd, "ip:", 3); \
    write(_file_fd, client_ip, strlen(client_ip)); \
    write(_file_fd, "  ", 2); \
    if ((work_status) > 0) { \
        write(_file_fd, "user connect success!", 22); \
    } else { \
        write(_file_fd, "user connect fail! ", 19); \
        fprintf(stderr, "%s error,connect fail", client_ip); \
    } \
    write(_file_fd, _time_buf, strlen(_time_buf)); \
} while(0)
#define operate_message_log(operation_name, client_ip, operation_context, operation_result) do { \
    int _file_fd = open("../log/log.txt", O_RDWR|O_CREAT|O_APPEND, 0644); \
    ERROR_CHECK(_file_fd, -1, "open log.txt"); \
    write(_file_fd, "ip:", 3); \
    write(_file_fd, client_ip, strlen(client_ip)); \
    time_t _time_sec = time(NULL); \
    char _time_buf[1024]; \
    ctime_r(&_time_sec, _time_buf); \
    write(_file_fd, "time:", 5); \
    write(_file_fd, _time_buf, strlen(_time_buf)); \
    switch(operation_name) { \
        case CD1: \
            write(_file_fd, "operation:cd ", 12); \
            break; \
        case LS1: \
            write(_file_fd, "operation:ls ", 12); \
            break; \
        case PWD1: \
            write(_file_fd, "operation:pwd ", 13); \
            break; \
        case REMOVE1: \
            write(_file_fd, "operation:rmdir ", 15); \
            break; \
        case MKDIR1: \
            write(_file_fd, "operation:mkdir ", 15); \
            break; \
        case PUTS1: \
            write(_file_fd, "operation:PUTS ", 14); \
            break; \
        case GETS1: \
            write(_file_fd, "operation:GETS ", 14); \
            break; \
    } \
    write(_file_fd, "context:", 8); \
    write(_file_fd, operation_context, strlen(operation_context)); \
    write(_file_fd, " ", 1); \
    if ((operation_result) > 0) { \
        write(_file_fd, "result:success", 14); \
    } else { \
        write(_file_fd, "result:failure", 14); \
    } \
    write(_file_fd, "\n", 1); \
} while(0)

/* int request_message_log(int work_status,char *client_ip){ */
/*     time_t time_sec = time(NULL); */
/*     char time_buf[1024]; */
/*     ctime_r(&time_sec,time_buf); */
/*     int file_fd = open("log.txt",O_RDWR|O_CREAT|O_APPEND,0644); */
/*     write(file_fd,"ip:",3); */
/*     write(file_fd,client_ip,strlen(client_ip)); */
/*     write(file_fd,"  ",2); */
/*    // strcat(time_buf,"\n"); */
/*     if(work_status>0){ */
/*         write(file_fd,"user connect success!",22); */
        
/*     }else{ */
/*         write(file_fd,"user connect fail! ",19); */

/*         fprintf(stderr,"%s error,connect fail",client_ip); */
            
/*     } */
/*     write(file_fd,time_buf,strlen(time_buf)); */
/*     return 0; */

/* }; */
/* int operate_message_log(int operation_name,char *client_ip,char *operation_context,int operation_result){ */
/*     int file_fd = open("log.txt",O_RDWR|O_CREAT|O_APPEND,0644); */
/*     ERROR_CHECK(file_fd,-1,"open log.txt"); */
/*     write(file_fd,"ip:",3); */
/*     write(file_fd,client_ip,strlen(client_ip)); */
/*     time_t time_sec = time(NULL); */
/*     char time_buf[1024]; */
/*     ctime_r(&time_sec,time_buf); */
/*     write(file_fd,"time:",5); */
/*     write(file_fd,time_buf,strlen(time_buf)); */
/*     switch(operation_name){ */
/*     case CD1: */
/*             write(file_fd,"operation:cd ",12); */
/*             break; */
/*     case LS1: */
/*             write(file_fd,"operation:ls ",12); */
/*             break; */
/*     case PWD1: */
/*             write(file_fd,"operation:pwd ",13); */
/*             break; */
/*     case REMOVE1: */
/*             write(file_fd,"operation:rmdir ",15); */
/*             break; */
/*     case MKDIR1: */
/*             write(file_fd,"operation:mkdir ",15); */
/*             break; */
/*     case PUTS1: */
/*             write(file_fd,"operation:PUTS ",14); */
/*             break; */
/*     case GETS1: */
/*             write(file_fd,"operation:GETS ",14); */
/*             break; */
/*   } */
/*               write(file_fd,"context:",8); */
/*               write(file_fd,operation_context,strlen(operation_context)); */
/*               write(file_fd," ",1); */
/*               if(operation_result>0){ */
/*                     write(file_fd,"result:success",14); */
/*               }else{ */
/*                   write(file_fd,"result:failure",14); */
/*               } */
/*      write(file_fd,"\n",1); */        
/*      return 0; */
/* } */

/* int main(){ */
/*     for(int i=1;i<=10;i++){ */
/*         sleep(1); */
/*     request_message_log(4,"192.168.1.128"); */
/*     operate_message_log(CD1,"192.168.198.128","hahahahaha",-1); */
/*     } */

/*  } */ 

